import { Router } from 'express';
import { classifyController } from '../controllers/classifyController';
import { 
  validateClassify, 
  validateClassifyUpdate, 
  validateBatchDelete 
} from '../middleware/validation';
import { 
  handleValidationErrors, 
  validateUUIDParam, 
  validateQueryParams,
  validateContentType 
} from '../middleware/validationHandler';
import rateLimit from 'express-rate-limit';

/**
 * 分类路由配置
 * 定义所有分类相关的API端点
 */
const router = Router();

// 分类操作速率限制
const classifyRateLimit = rateLimit({
  windowMs: 15 * 60 * 1000, // 15分钟
  max: 100, // 限制每个IP在窗口期内最多100次请求
  message: {
    success: false,
    error: {
      code: 'RATE_LIMIT_EXCEEDED',
      message: '请求过于频繁，请稍后再试'
    }
  },
  standardHeaders: true,
  legacyHeaders: false
});

// 创建和更新操作的严格速率限制
const strictRateLimit = rateLimit({
  windowMs: 15 * 60 * 1000, // 15分钟
  max: 20, // 限制每个IP在窗口期内最多20次创建/更新请求
  message: {
    success: false,
    error: {
      code: 'RATE_LIMIT_EXCEEDED',
      message: '创建/更新操作过于频繁，请稍后再试'
    }
  },
  standardHeaders: true,
  legacyHeaders: false
});

/**
 * @route GET /api/classifies/stats
 * @desc 获取分类统计信息
 * @access Public
 */
router.get('/stats', classifyRateLimit, classifyController.getClassifyStats.bind(classifyController));

/**
 * @route GET /api/classifies
 * @desc 获取所有分类（支持分页和搜索）
 * @access Public
 * @query page - 页码（可选，默认1）
 * @query limit - 每页数量（可选，默认10）
 * @query search - 搜索关键词（可选）
 * @query sortBy - 排序字段（可选，默认createdAt）
 * @query sortOrder - 排序方向（可选，默认desc）
 */
router.get('/', classifyRateLimit, validateQueryParams, classifyController.getAllClassifies.bind(classifyController));

/**
 * @route GET /api/classifies/:id
 * @desc 根据ID获取单个分类
 * @access Public
 * @param id - 分类ID
 */
router.get('/:id', classifyRateLimit, validateUUIDParam('id'), classifyController.getClassifyById.bind(classifyController));

/**
 * @route POST /api/classifies
 * @desc 创建新分类
 * @access Public
 * @body CreateClassifyRequest
 */
router.post('/', 
  strictRateLimit,
  validateContentType,
  validateClassify,
  handleValidationErrors,
  classifyController.createClassify.bind(classifyController)
);

/**
 * @route PUT /api/classifies/:id
 * @desc 更新分类
 * @access Public
 * @param id - 分类ID
 * @body UpdateClassifyRequest
 */
router.put('/:id', 
  strictRateLimit,
  validateUUIDParam('id'),
  validateContentType,
  validateClassifyUpdate,
  handleValidationErrors,
  classifyController.updateClassify.bind(classifyController)
);

/**
 * @route DELETE /api/classifies/:id
 * @desc 删除单个分类
 * @access Public
 * @param id - 分类ID
 */
router.delete('/:id', 
  strictRateLimit,
  validateUUIDParam('id'),
  classifyController.deleteClassify.bind(classifyController)
);

/**
 * @route DELETE /api/classifies
 * @desc 批量删除分类
 * @access Public
 * @body { ids: string[] }
 */
router.delete('/', 
  strictRateLimit,
  validateContentType,
  validateBatchDelete,
  handleValidationErrors,
  classifyController.batchDeleteClassifies.bind(classifyController)
);

export default router;